//============================================================================
// Copyright (C) 2013 Brett R. Jones
//
// Issued to public domain 2013
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================

#include <memory.h>
#include "VxPktHdr.h"

//============================================================================
//=== constructor ===//
VxPktHdrPrefix::VxPktHdrPrefix()
    : m_u16PktLen(0)
    , m_u16PktType(0)
    //, m_u8SeqNum(0) no need to set this
    , m_u8PktVersion(1)
    , m_u16PktExtra(0)
{
    //empty
}

//============================================================================
//=== return true if valid pkt type and length ===//
bool VxPktHdrPrefix::IsValidVxPkt( void )
{
    if( (16 > m_u16PktLen) ||
        (m_u16PktLen > MAX_PKT_LEN ) ||
        (m_u16PktLen & 0x0f) ||
        (1 > m_u16PktType) ||
        (250 < m_u16PktType) )
    {
        return false;
    }
    return true;
}

//============================================================================
//! return true if data length is large enough to contain this packet
bool VxPktHdrPrefix::IsPktAllHere(int iDataLen)
{
    return (iDataLen >= m_u16PktLen)?1:0;
}

//============================================================================
//! return length of packet
U16 VxPktHdrPrefix::GetPktLength( void )
{
    return m_u16PktLen;
}

//============================================================================
//! return packet type
U16	VxPktHdrPrefix::getPktTypeAndPluginNum( void )
{
    return m_u16PktType;
}
//============================================================================
U8	VxPktHdrPrefix::getPktType( void )
{
	return (U8)(m_u16PktType & 0xff);
}

//============================================================================
U8	VxPktHdrPrefix::getPluginNum( void )
{
	return (U8)(m_u16PktType >> 8);
}
//============================================================================
OnlineId& VxPktHdr::getSrcOnlineId()
{
    return m_oSrcOnlineId;
}

//============================================================================
OnlineId& VxPktHdr::getDestOnlineId()
{
    return m_oDestOnlineId;
}

//============================================================================
//! make a copy of this packet
VxPktHdr * VxPktHdr::MakeCopy( void )
{
    U8 * pu8Copy = new U8[ m_u16PktLen ];
    memcpy( pu8Copy, this, m_u16PktLen );
    return (VxPktHdr *)pu8Copy;
}
